/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.classdiagram.parsers.modelgenerator;

import java.io.StringReader;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.TemplateableElement;

import antlr.RecognitionException;
import antlr.TokenStreamException;

import com.cea.papyrus.classdiagram.parsers.PapyrusParsersPlugin;
import com.cea.papyrus.classdiagram.parsers.antlr.PropertyLabelLexer;
import com.cea.papyrus.classdiagram.parsers.antlr.PropertyLabelParser;
import com.cea.papyrus.classdiagram.parsers.util.TypeRecognitionException;
import com.cea.papyrus.classdiagram.parsers.util.UnboundTemplateRecognitionException;

/**
 * 
 * 
 * @author Remi SCHNEKENBURGER
 * CEA-List
 */
public class PropertyGenerator {

	/**
	 * 
	 */
	private Property property;

	/**
	 * Default Constructor.
	 * 
	 * @param property the property to modify
	 */
	public PropertyGenerator(Property property) {
		this.property = property;
	}

	/**
	 * Parse the label of the property and modify property attributes.
	 * @param label the label that defines the property
	 */
	public void parseAndModifyProperty(String label) {
		try {
			PropertyLabelLexer lexer = new PropertyLabelLexer(new StringReader(label));
			PropertyLabelParser parser = new PropertyLabelParser(lexer, property);
			parser.label();
		} catch (RecognitionException e){
			PapyrusParsersPlugin.getDefault().getLog().log(
					new Status(IStatus.ERROR,
							PapyrusParsersPlugin.PLUGIN_ID,
							IStatus.OK,
							"could not parse the property label: "+label,
							e));
		} catch (TokenStreamException e){
			PapyrusParsersPlugin.getDefault().getLog().log(
					new Status(IStatus.ERROR,
							PapyrusParsersPlugin.PLUGIN_ID,
							IStatus.OK,
							"could not parse the property label: "+label,
							e));
		}
	}

	/**
	 * Parse the label of the property and validate it.
	 * 
	 * @param label the label that defines the property
	 * 
	 * @return null if label is valid, else return the message that describes the error.
	 */
	public String parseAndValidateProperty(String label) {
		String message = null;

		// to validate the message : parse it. If no errors, it is ok. If exceptions : not ok. Returns the exception message
		PropertyLabelLexer lexer = new PropertyLabelLexer(new StringReader(label));
		PropertyLabelParser parser = new PropertyLabelParser(lexer, property);
		parser.setValidation(true);
		try {
			parser.label();
		} catch (RecognitionException e) {
			message = e.getLocalizedMessage();
		} catch (TokenStreamException e) {
			message = e.getLocalizedMessage();
		}

		return message;
	}
	
	/**
	 * Parse the label of the property, and generates an exception if does not validate.
	 * 
	 * @param label the label that defines the property
	 * 
	 * @return null if label is valid, else return the message that describes the error.
	 */
	public String parseUndefinedPropertyType(String label) {

		// to validate the message : parse it. If no errors, it is ok. If exceptions : not ok. Returns the exception message
		PropertyLabelLexer lexer = new PropertyLabelLexer(new StringReader(label));
		PropertyLabelParser parser = new PropertyLabelParser(lexer, property);
		parser.setValidation(true);
		try {
			parser.label();
		}
		catch (RecognitionException re) {
			if (re instanceof TypeRecognitionException)
				return ((TypeRecognitionException)re).getTypeName() ;
		}
		catch (TokenStreamException tse) {
		}
		return null ;
	}
	
	/**
	 * Parse the label of the property, and generates an exception if does not validate.
	 * 
	 * @param label the label that defines the property
	 * 
	 * @return null if label is valid, else return the message that describes the error.
	 */
	public TemplateableElement parseUnboundPropertyType(String label) {

		// to validate the message : parse it. If no errors, it is ok. If exceptions : not ok. Returns the exception message
		PropertyLabelLexer lexer = new PropertyLabelLexer(new StringReader(label));
		PropertyLabelParser parser = new PropertyLabelParser(lexer, property);
		parser.setValidation(true);
		try {
			parser.label();
		}
		catch (RecognitionException re) {
			if (re instanceof UnboundTemplateRecognitionException)
				return ((UnboundTemplateRecognitionException)re).getTemplate() ;
		}
		catch (TokenStreamException tse) {
		}
		return null ;
	}
}
